package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.CustomException;
import com.itheima.mapper.CategoryDao;
import com.itheima.mapper.DishDao;
import com.itheima.mapper.EmployeeDao;
import com.itheima.mapper.SetmealDao;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.result.R;
import com.itheima.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 斌
 * @DateTime: 2023年02月06日  20:38:05
 * @Description: com.itheima.service.Impl
 * @Version: 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private DishDao dishDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public R addCategory(Category category, HttpSession session) {
        if (category != null){
            int result = categoryDao.insert(category);
            return result > 0 ? R.success("添加成功") : R.error("添加失败");
        }
        return R.error("数据异常，请重试");

    }

    @Override
    public R selectCategory(Integer page, Integer pageSize,Category category) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        Page<Category> selectPage = categoryDao.selectPage(categoryPage, null);
        return R.success(selectPage);
    }

    @Override
    public R updateCategory(Category category, HttpSession session) {
        if ( category != null ){
            int result = categoryDao.updateById(category);
            return result > 0 ? R.success("修改成功") : R.error("修改失败");
        }
        return R.error("数据异常，请稍后重试");
    }

    @Override
    public R deleteCategory(Long id) {

        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.lambda().eq(Dish::getCategoryId,id);
        Integer dishCount = dishDao.selectCount(dishQueryWrapper);
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.lambda().eq(Setmeal::getCategoryId,id);
        Integer setmealCount = setmealDao.selectCount(setmealQueryWrapper);
        if (dishCount > 0 ){
            throw new CustomException("当前分类下有菜品，不能删除");
        }
        if (setmealCount > 0 ){
           throw new CustomException("当前分类下有套餐，不能删除");
        }
        int result = categoryDao.deleteById(id);
        return result > 0 ? R.success("删除成功") : R.error("删除失败");
    }

    @Override
    public R listCategory(Category category) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //type不等于空，查询菜品或者套餐的分类列表
        //type等于空，查询菜品和套餐的分类列表
        queryWrapper.lambda().eq(category.getType()!=null,Category::getType,category.getType()).orderByDesc(Category::getSort);
        List<Category> categories = categoryDao.selectList(queryWrapper);

        return categories != null ? R.success(categories) : R.error("该菜品分类下无数据");
    }
}
